package com.zzsy.carosshopprojects.activitys.home.mainpage.common.scan;

import com.king.base.util.ToastUtils;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.integral.IntegralAc;
import com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.invoice.ReceiptListAc;
import com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.maincardmanager.DelCouponAc;
import com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.memberrecharge.MemberRechargeAc;
import com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.operationmanual.OperationManualAc;
import com.zzsy.carosshopprojects.activitys.home.mainpage.member.MemberAc;
import com.zzsy.carosshopprojects.base.BaseViewMode;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.MemberInfoBean;
import com.zzsy.carosshopprojects.bean.TokenBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.StringUtils;

import androidx.lifecycle.MutableLiveData;

public class ScanMode extends BaseViewMode {
    private MutableLiveData<MemberInfoBean> live;
    private MutableLiveData<TokenBean> liveToken;

    public MutableLiveData<MemberInfoBean> getLive() {
        if (live == null)
            live = new MutableLiveData<>();
        return live;
    }

    public MutableLiveData<TokenBean> getliveToken() {
        if (live == null)
            liveToken = new MutableLiveData<>();
        return liveToken;
    }

    private MutableLiveData<Integer> liveCode;

    public MutableLiveData<Integer> getLiveCode() {
        if (liveCode == null)
            liveCode = new MutableLiveData<>();
        return liveCode;
    }

    /**
     * 根据UUID查询会员信息
     *
     * @param context
     * @param uuid
     * @param num
     */
    public void MemberByUuid(RxAppCompatActivity context, String uuid, int num) {
        RequestUtils.MemberByUuid(context, uuid, DataUtil.getInstance().getCompanyId(), new MyObserver<MemberInfoBean>(context, false) {
            @Override
            public void onSuccess(MemberInfoBean bean) {
                String memberId = bean.getMemberId();
                String parentId = bean.getParentId();
                String memberCompanyId = bean.getCompanyId();
                if (StringUtils.isEmpty(memberCompanyId)) {
                    memberCompanyId = "";
                }
                if (StringUtils.isEmpty(memberId)) {
                    ToastUtils.showToast(context.getApplicationContext(), context.getApplicationContext().getResources().getString(R.string.thisCardNoMember));
                    context.finish();
                } else {
                    switch (num) {
                        case 1:
                            //会员消费
                            MemberAc.start(context, uuid);
                            context.finish();
                            break;
                        case 3:
                            // 会员充值
                            if (!memberCompanyId.equals(DataUtil.getInstance().getCompanyId())) {
                                ToastUtils.showToast(context.getApplicationContext(), context.getApplicationContext().getResources().getString(R.string.NoCrossGroup));
                                context.finish();
                                return;
                            }
                            if (!StringUtils.isEmpty(parentId)) {
                                boolean subCard = isSubCard(parentId);
                                if (subCard) {
                                    ToastUtils.showToast(context.getApplicationContext(), context.getApplicationContext().getResources().getString(R.string.SubCardNoSuchFunction));
                                    context.finish();
                                    return;
                                } else {
                                    MemberRechargeAc.start(context, uuid);
                                    context.finish();
                                }
                            }
                            break;
                        case 8:
                            //优惠券核销
                            if (!StringUtils.isEmpty(parentId)) {
                                boolean subCard = isSubCard(parentId);
                                if (subCard) {
                                    ToastUtils.showToast(context.getApplicationContext(), "副卡暂无该功能！");
                                    context.finish();
                                    return;
                                } else {
                                    OperationManualAc.start(context, uuid);
                                    context.finish();
                                }
                            }
                            break;
                        case 10:
                            //扣除积分
                            if (!StringUtils.isEmpty(parentId)) {
                                boolean subCard = isSubCard(parentId);
                                if (subCard) {
                                    ToastUtils.showToast(context.getApplicationContext(), "副卡暂无该功能！");
                                    context.finish();
                                    return;
                                } else {
                                    IntegralAc.start(context, uuid);
                                    context.finish();
                                }
                            }
                            break;
                        case 12:
                            if (!StringUtils.isEmpty(parentId)) {
                                boolean subCard = isSubCard(parentId);
                                if (subCard) {
                                    ToastUtils.showToast(context.getApplicationContext(), "副卡暂无该功能！");
                                    context.finish();
                                    return;
                                } else {
                                    //开具发票
                                    ReceiptListAc.start(context, uuid);
                                    context.finish();
                                }
                            }
                            break;
                        case 13:
                            if (!StringUtils.isEmpty(parentId)) {
                                boolean subCard = isSubCard(parentId);
                                if (subCard) {
                                    ToastUtils.showToast(context.getApplicationContext(), "副卡暂无该功能！");
                                    context.finish();
                                    return;
                                } else {
                                    //删除优惠券
                                    DelCouponAc.start(context, uuid);
                                    context.finish();
                                }
                            }
                            break;
                    }


                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context.getApplicationContext(), errorMsg);
                context.finish();
            }
        });
    }

    /**
     * 获取Token
     *
     * @param context
     */
    public void getToken(RxAppCompatActivity context, String result) {
        RequestUtils.getToken(context, new MyObserver<BaseBean>(context) {
            @Override
            public void onSuccess(BaseBean result) {

            }

            @Override
            public void onFailure(Throwable e, String tokenMsg, int code) {
                if (code == ConfigureFile.code) {
                    TokenBean bean = new TokenBean();
                    bean.setToken(tokenMsg);
                    bean.setResult(result);
                    liveToken.postValue(bean);
                } else {
                    ToastUtils.showToast(context.getApplicationContext(), tokenMsg);
                    context.finish();
                }

            }
        });
    }

    /**
     * 开具发票接口
     *
     * @param context
     * @param result
     * @param token
     */
    public void DrawBill(RxAppCompatActivity context, String result, String token) {
        RequestUtils.DrawBill(context, result, token, new MyObserver<BaseBean>(context) {
            @Override
            public void onSuccess(BaseBean result) {
                liveCode.postValue(200);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                if (code == ConfigureFile.code) {
//                    Intent intent = new Intent(ScanActivity.this, ReceiptSufActivity.class);
//                    startActivity(intent);
//                    context.finish();
                    liveCode.postValue(200);
                } else {
                    ToastUtils.showToast(context.getApplicationContext(), errorMsg);
                    context.finish();
                }
            }
        });
    }

    /**
     * 验证主卡还是副卡
     */
    private boolean isSubCard(String parentId) {
        if (!"0".equals(parentId)) {
            return true;
        } else {
            return false;
        }
    }

}
